//
//  BaseTabBarController.m
//  BaseFrame
//
//  Created by 孟学浩 on 16/9/28.
//  Copyright © 2016年 com.mxh. All rights reserved.
//

#import "BaseTabBarController.h"
#import "LoginViewController.h"
#import "MyTabBar.h"

@interface BaseTabBarController ()<UITabBarControllerDelegate,MyTabBarDelegate>

@property (nonatomic,strong) MyTabBar *myTabBar;

@end

@implementation BaseTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, -3)];
    
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:getColor(@"303030")} forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:getColor(mainColor)} forState:UIControlStateSelected];
    
    CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, 20);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    
    [self setTabBarItemWithIndex:0
                  nomalImageName:@"home_nor"
               selectedImageName:@"home_sel"
                           title:@"首页"];
    [self setTabBarItemWithIndex:1
                  nomalImageName:@"mall_nor"
               selectedImageName:@"mall_sel"
                           title:@"商城"];
    [self setTabBarItemWithIndex:2
                  nomalImageName:@""
               selectedImageName:@""
                           title:@""];
    [self setTabBarItemWithIndex:3
                  nomalImageName:@"TheOrder_nor"
               selectedImageName:@"TheOrder_sel"
                           title:@"订单"];
    [self setTabBarItemWithIndex:4
                  nomalImageName:@"my_nor"
               selectedImageName:@"my_sel"
                           title:@"我的"];
    
    
    self.myTabBar = [[MyTabBar alloc] init];
    [self.myTabBar setBackgroundImage:img];
    [self.myTabBar setShadowImage:img];
    //取消tabBar的透明效果
    self.myTabBar.translucent = NO;
    self.myTabBar.myDelegate = self;
    // KVC：如果要修系统的某些属性，但被设为readOnly，就是用KVC，即setValue：forKey：。
    [self setValue:self.tabBar forKey:@"tabBar"];
}


/**
 *  设置item图片和文字
 *
 *  @param index             索引
 *  @param nomalImageName    未选择图片
 *  @param selectedImageName 选中图片
 *  @param title             显示文字
 */
- (void) setTabBarItemWithIndex:(NSInteger)index nomalImageName:(NSString*)nomalImageName selectedImageName:(NSString*)selectedImageName title:(NSString *)title{
    UITabBarItem *item = self.tabBar.items[index];
    //设置常规图片
    if (nomalImageName) {
        UIImage *imageNomal = [[UIImage imageNamed:nomalImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        item.image = imageNomal;
    }
    //设置选中图片
    UIImage *imageSelected = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    item.selectedImage = imageSelected;
    item.title = title;

}

- (void)tabBarDidClickPlusButton:(MyTabBar *)tabBar{
    
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
    
    NSUInteger i = [tabBarController.viewControllers indexOfObject:viewController];
    if (i==2) {
        self.myTabBar.plusBtn.selected = YES;
        self.myTabBar.titleLabel.textColor = getColor(mainColor);
    }else{
        self.myTabBar.plusBtn.selected = NO;
        self.myTabBar.titleLabel.textColor = getColor(@"303030");
    }
    
//    switch ([tabBarController.viewControllers indexOfObject:viewController]) {
//        case 0:{
//            return YES;
//        }break;
//        default:{
//            if ([[UserModel sharedInstanced].mobile isEqualToString:@""]) {
//                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginViewController"]];
//                nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//                [self presentViewController:nav animated:YES completion:nil];
//                [self.navigationController popToRootViewControllerAnimated:YES];
//                ((LoginViewController *)[nav.viewControllers firstObject]).loginSuccessHandle = ^(){
//                    [self setSelectedIndex:[tabBarController.viewControllers indexOfObject:viewController]];
//                };
//                return NO;
//            }else{
//                return YES;
//            }
//            return YES;
//        }break;
//    }
    return YES;
}

@end
